package com.yougou.ui.category;

import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yougou.R;
import com.yougou.base.BaseFragment;
import com.yougou.data.model.Category;
import com.yougou.ui.category.adapter.CategoryAdapter;
import com.yougou.ui.category.adapter.CategoryGoodsAdapter;
import com.yougou.ui.category.adapter.SubCategoryAdapter;
import com.yougou.utils.ToastUtils;

import java.util.List;

public class CategoryFragment extends BaseFragment {
    
    private RecyclerView mCategoryRecyclerView;
    private RecyclerView mSubCategoryRecyclerView;
    private RecyclerView mGoodsRecyclerView;
    private View mDivider;
    
    private CategoryViewModel mViewModel;
    private CategoryAdapter mCategoryAdapter;
    private SubCategoryAdapter mSubCategoryAdapter;
    private CategoryGoodsAdapter mGoodsAdapter;
    
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category;
    }
    
    @Override
    protected void initView(View view) {
        mCategoryRecyclerView = view.findViewById(R.id.rv_category);
        mSubCategoryRecyclerView = view.findViewById(R.id.rv_sub_category);
        mGoodsRecyclerView = view.findViewById(R.id.rv_goods);
        mDivider = view.findViewById(R.id.divider);
        
        // 初始化一级分类列表
        mCategoryRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mCategoryAdapter = new CategoryAdapter();
        mCategoryRecyclerView.setAdapter(mCategoryAdapter);
        
        // 初始化二级/三级分类列表（横向滚动）
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), 
                LinearLayoutManager.HORIZONTAL, false);
        mSubCategoryRecyclerView.setLayoutManager(layoutManager);
        mSubCategoryAdapter = new SubCategoryAdapter();
        mSubCategoryRecyclerView.setAdapter(mSubCategoryAdapter);
        
        // 初始化商品网格
        mGoodsRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mGoodsAdapter = new CategoryGoodsAdapter();
        mGoodsRecyclerView.setAdapter(mGoodsAdapter);
        
        // 一级分类点击事件
        mCategoryAdapter.setOnItemClickListener((category, position) -> {
            mCategoryAdapter.setSelectedPosition(position);
            handleCategoryClick(category);
        });
        
        // 二级/三级分类点击事件
        mSubCategoryAdapter.setOnItemClickListener((category, position) -> {
            handleSubCategoryClick(category);
        });
    }
    
    
    private void handleCategoryClick(Category category) {
        List<Category> children = category.getChildren();
        if (children != null && !children.isEmpty()) {
            // 有子分类，显示二级分类
            mSubCategoryRecyclerView.setVisibility(View.VISIBLE);
            mDivider.setVisibility(View.VISIBLE);
            mSubCategoryAdapter.setData(children);
            
            // 默认选中第一个二级分类
            handleSubCategoryClick(children.get(0));
        } else {
            // 没有子分类，直接显示该分类的商品
            mSubCategoryRecyclerView.setVisibility(View.GONE);
            mDivider.setVisibility(View.GONE);
            mViewModel.filterGoodsByCategory(category);
        }
    }
    
    
    private void handleSubCategoryClick(Category category) {
        // 二级分类可能还有三级分类（children）
        List<Category> children = category.getChildren();
        if (children != null && !children.isEmpty()) {
            // 有三级分类，更新显示为三级分类
            mSubCategoryAdapter.setData(children);
            // 默认选中第一个三级分类并显示商品
            mViewModel.filterGoodsByCategory(children.get(0));
        } else {
            // 没有三级分类，直接显示该二级分类的商品
            mViewModel.filterGoodsByCategory(category);
        }
    }
    
    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this).get(CategoryViewModel.class);
        
        // 观察分类数据
        mViewModel.getCategoryList().observe(getViewLifecycleOwner(), categories -> {
            if (categories != null && !categories.isEmpty()) {
                mCategoryAdapter.setData(categories);
                // 默认选中第一个分类并显示其内容
                handleCategoryClick(categories.get(0));
            }
        });
        
        // 观察商品数据
        mViewModel.getGoodsList().observe(getViewLifecycleOwner(), goods -> {
            if (goods != null) {
                mGoodsAdapter.setData(goods);
            }
        });
        
        // 观察错误信息
        mViewModel.getError().observe(getViewLifecycleOwner(), error -> {
            if (error != null) {
                ToastUtils.show(requireContext(), error);
            }
        });
        
        // 设置商品点击事件
        mGoodsAdapter.setOnItemClickListener(goods -> {
            // 跳转到商品详情页
            android.content.Intent intent = new android.content.Intent(requireContext(), com.yougou.ui.product.ProductDetailActivity.class);
            intent.putExtra(com.yougou.ui.product.ProductDetailActivity.EXTRA_GOODS_ID, goods.getGoodsId());
            startActivity(intent);
        });
        
        // 加载分类数据
        mViewModel.loadCategories();
    }
}
